﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using TeF.Runner.Helpers;

namespace TeF.Runner
{
    internal class TestAssembly : TestStateEntityBase, ITestAssembly, ITestEntityParentOfT<TestAssembly, TestNamespace>
    {
        #region Fields

        private readonly TestParentHelper<TestAssembly, TestNamespace> _testParentHelper;

        #endregion


        #region Constructors

        public TestAssembly(Assembly assembly, bool ignored)
            : base(ignored)
        {
            if (assembly == null)
                throw new ArgumentNullException("assembly");

            Assembly = assembly;

            _testParentHelper = new TestParentHelper<TestAssembly, TestNamespace>(this);
        }
        
        #endregion


        #region Properties

        public override string Name
        {
            get { return Assembly.GetName().Name; }
        }

        public Assembly Assembly { get; private set; }

        public IEnumerable<ITestNamespace> Namespaces
        {
            get { return _testParentHelper.Children; }
        }

        public IEnumerable<TestNamespace> Children
        {
            get { return _testParentHelper.Children; }
        }
        
        #endregion


        #region Public Methods

        public void AddChild(TestNamespace child)
        {
            _testParentHelper.AddChild(child);
        }

        public void Dispose()
        {
            _testParentHelper.Dispose();
        }

        public IEnumerable<Test> GetTestsToRun()
        {
            if (Ignored)
                return new Test[] { };

            return
                _testParentHelper.Children
                    .Where(c => !c.Ignored)
                    .SelectMany(c => c.GetTestsToRun());

        }

        #endregion
    }
}
